package com.sxm.yiwei.fragments;


import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.AddCloseActivity;
import com.sxm.yiwei.activitis.MineAccount;
import com.sxm.yiwei.activitis.MineAdresser;
import com.sxm.yiwei.activitis.MineAskNumberActivty;
import com.sxm.yiwei.activitis.MineHelp;
import com.sxm.yiwei.activitis.MineInstall;
import com.sxm.yiwei.activitis.MineMember;
import com.sxm.yiwei.activitis.MineMessage;
import com.sxm.yiwei.activitis.MineMoney;
import com.sxm.yiwei.activitis.MineNotification;
import com.sxm.yiwei.activitis.MineRank;
import com.sxm.yiwei.activitis.MineRecipients;
import com.sxm.yiwei.activitis.MineServicer;
import com.sxm.yiwei.activitis.MineView;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/** 我的页面
 * A simple {@link Fragment} subclass.
 */

public class MineFragment extends Fragment implements View.OnClickListener{
    private String TAG = "tag";
    private RelativeLayout message;
    private LinearLayout account;
    private LinearLayout addresser;
    private LinearLayout help;
    private LinearLayout install;
    private LinearLayout money;
    private LinearLayout servicer;
    private LinearLayout view;
    private LinearLayout recipients;
    private LinearLayout rank;
    private LinearLayout member;
    private LinearLayout notification;
    private ImageView headimg;
    private TextView username;
    private ImageView usergrade;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String avatar;
    private String gradeCode;
    private String identityStatus;
    private String nickname;
    private Bitmap bitmap;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    username.setText(nickname);
                    Picasso.with(getActivity()).load(InternetAddressUtis.IMG_URL+avatar).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(headimg);
                    break;
            }
        }
    };
    private SharedPreferences sss;
    private Context context;
    private ImageView gredeimg;
    private String shopType;
    private String sellGrade;
    private ImageView xinimg;
    private Integer sell;
    private String orderCount;
    private TextView xinnum;
    private TextView booknumber;
    private String isInsurance;
    private ImageView baoimg;
    private TextView numbert;
    private String shopAuditStatus;

    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            return;
        }else {
            getUserData();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    private void initMineView(View view1) {
        message = (RelativeLayout) view1.findViewById(R.id.mine_message);
        account = (LinearLayout) view1.findViewById(R.id.mine_account);
        help = (LinearLayout) view1.findViewById(R.id.mine_help);
        install = (LinearLayout) view1.findViewById(R.id.mine_install);
        money = (LinearLayout) view1.findViewById(R.id.mine_money);
        servicer = (LinearLayout) view1.findViewById(R.id.mine_servicer);
        view = (LinearLayout) view1.findViewById(R.id.mine_view);
        recipients = (LinearLayout) view1.findViewById(R.id.mine_recipients);
        rank = (LinearLayout)view1.findViewById(R.id.mine_rank);
        member = (LinearLayout) view1.findViewById(R.id.mine_member);
        notification = (LinearLayout) view1.findViewById(R.id.mine_notification);
        gredeimg = (ImageView) view1.findViewById(R.id.mine_grade);
        xinimg = (ImageView) view1.findViewById(R.id.mine_two);
        xinnum = (TextView) view1.findViewById(R.id.mine_xinnum);
        booknumber = (TextView) view1.findViewById(R.id.mine_booknumber);
        baoimg = (ImageView) view1.findViewById(R.id.mine_baoimg);
        numbert = (TextView) view1.findViewById(R.id.mine_numbertext);


        // 用户信息
        headimg = (ImageView) view1.findViewById(R.id.mine_userheadimg);
        username = (TextView) view1.findViewById(R.id.mine_username);
        usergrade = (ImageView) view1.findViewById(R.id.mine_usergrade);
        // 设置点击事件
        message.setOnClickListener(this);
        account.setOnClickListener(this);
        help.setOnClickListener(this);
        install.setOnClickListener(this);
        money.setOnClickListener(this);
        servicer.setOnClickListener(this);
        servicer.setOnClickListener(this);
        view.setOnClickListener(this);
        recipients.setOnClickListener(this);
        rank.setOnClickListener(this);
        member.setOnClickListener(this);
        notification.setOnClickListener(this);

    }

    @Override
    public void onStart() {
        super.onStart();
        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        getUserData();

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view1 = inflater.inflate(R.layout.fragment_mine, container, false);
        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");

        // 初始化控件
        initMineView(view1);
        getUserData();
        return view1;

    }
    // 获取用户信息
    private void getUserData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETUSERINFO);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 个人信息--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                sss = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sss.edit();
                edit.putString("avatar",avatar);
                edit.commit();
                avatar = obj1.getString("avatar");//头像
                shopType = obj1.getString("shopType");//店铺类型
                sellGrade = obj1.getString("sellGrade");//店铺类型
                gradeCode = obj1.getString("gradeCode");// 等级
                orderCount = obj1.getString("orderCount");// 发单数量
                identityStatus = obj1.getString("identityStatus");//身份审核状态
                nickname = obj1.getString("nickname");//昵称
                shopType = obj1.getString("shopType");//昵称
                isInsurance = obj1.getString("isInsurance");//是否身份审核
                shopAuditStatus = obj1.getString("shopAuditStatus");//是否身份审核
                handler.sendEmptyMessage(1);
                if (identityStatus.equals("1")){
                    numbert.setText("升级一喂员");
                }else {}
                if (isInsurance!=null){
                    if (isInsurance.equals("1")){
                        baoimg.setImageResource(R.mipmap.baocai);
                    }else {
                        baoimg.setImageResource(R.mipmap.baohui);
                    }
                }else {}
                        if (shopAuditStatus!=null){
                            if (shopAuditStatus.equals("1")){
                                if (shopType!=null){
                                    if (shopType.equals("1")){
                                        usergrade.setVisibility(View.GONE);
                                    }else {
                                        usergrade.setVisibility(View.VISIBLE);
                                    }

                                }else {
                                    usergrade.setVisibility(View.GONE);
                                }

                            }else {
                                usergrade.setVisibility(View.GONE);
                            }
                        }else {
                            usergrade.setVisibility(View.GONE);
                        }
                if (booknumber==null){
                    booknumber.setText("0");
                }else {
                    booknumber.setText(orderCount);
                }
                if (sellGrade==null){

                }else {
                    sell = Integer.valueOf(sellGrade);
                    switch (sell){
                        case 0:
                            xinimg.setVisibility(View.INVISIBLE);
                            break;
                        case 1:
                            xinimg.setImageResource(R.mipmap.xin);
                            break;
                        case 2:
                            xinnum.setText("2");
                            xinimg.setImageResource(R.mipmap.xin);
                            break;
                        case 3:
                            xinnum.setText("3");
                            xinimg.setImageResource(R.mipmap.xin);
                            break;
                        case 4:
                            xinnum.setText("4");
                            xinimg.setImageResource(R.mipmap.xin);
                            break;
                        case 5:
                            xinnum.setText("5");
                            xinimg.setImageResource(R.mipmap.xin);
                            break;
                        case 6:
                            xinimg.setImageResource(R.mipmap.zuan);
                            break;
                        case 7:
                            xinnum.setText("2");
                            xinimg.setImageResource(R.mipmap.zuan);
                            break;
                        case 8:
                            xinnum.setText("3");
                            xinimg.setImageResource(R.mipmap.zuan);
                            break;
                        case 9:
                            xinnum.setText("4");
                            xinimg.setImageResource(R.mipmap.zuan);
                            break;
                        case 10:
                            xinnum.setText("5");
                            xinimg.setImageResource(R.mipmap.zuan);
                            break;
                        case 11:

                            xinimg.setImageResource(R.mipmap.gaun);
                            break;
                        case 12:
                            xinnum.setText("2");
                            xinimg.setImageResource(R.mipmap.gaun);
                            break;
                        case 13:
                            xinnum.setText("3");
                            xinimg.setImageResource(R.mipmap.gaun);
                            break;
                        case 14:
                            xinnum.setText("4");
                            xinimg.setImageResource(R.mipmap.gaun);
                            break;
                        case 15:
                            xinnum.setText("5");
                            xinimg.setImageResource(R.mipmap.gaun);
                            break;
                        case 16:
                            xinimg.setImageResource(R.mipmap.jinguan);
                            break;
                        case 17:
                            xinnum.setText("2");
                            xinimg.setImageResource(R.mipmap.jinguan);
                            break;
                        case 18:
                            xinnum.setText("3");
                            xinimg.setImageResource(R.mipmap.jinguan);
                            break;
                        case 19:
                            xinnum.setText("4");
                            xinimg.setImageResource(R.mipmap.jinguan);
                            break;
                        case 20:
                            xinnum.setText("5");
                            xinimg.setImageResource(R.mipmap.jinguan);
                            break;
                    }
                }


                switch (Integer.valueOf(gradeCode)){
                    case 0:
                        gredeimg.setImageResource(R.mipmap.shahuise);
                        break;
                    case 1:
                       gredeimg.setImageResource(R.mipmap.g10);
                        break;
                    case 2:
                        gredeimg.setImageResource(R.mipmap.g1);
                        break;
                    case 3:
                        gredeimg.setImageResource(R.mipmap.g2);
                        break;
                    case 4:
                        gredeimg.setImageResource(R.mipmap.g3);
                        break;
                    case 5:
                        gredeimg.setImageResource(R.mipmap.g4);
                        break;
                    case 6:
                        gredeimg.setImageResource(R.mipmap.g6);
                        break;
                    case 7:
                        gredeimg.setImageResource(R.mipmap.g5);
                        break;
                    case 8:
                        gredeimg.setImageResource(R.mipmap.g7);
                        break;
                    case 9:
                        gredeimg.setImageResource(R.mipmap.g8);
                        break;
                    case 10:
                        gredeimg.setImageResource(R.mipmap.g9);
                        break;

                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 个人信息--"+ex);
              //  ErrType.errType(getActivity(),ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            }
        });
    }

    // 点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_message://个人中心
                MainActivity.a = avatar;
                startActivity(new Intent(getActivity(),MineMessage.class));
                break;
            case R.id.mine_notification:// 公告
                startActivity(new Intent(getActivity(), MineNotification.class));
                break;
            case R.id.mine_account://账户
                Intent accountIntent = new Intent(getActivity(), MineAccount.class);
                accountIntent.putExtra("identityStatus",identityStatus);
                startActivity(accountIntent);
                break;
            case R.id.mine_member://会员

                if (identityStatus==null){
                    // 身份认证页面
                    startActivity(new Intent(getActivity(), MineMember.class));
                }else {
                    // 判断跳转
                    if (identityStatus.equals("0")) {
                        // 身份认证页面
                        startActivity(new Intent(getActivity(), MineMember.class));

                    } else if (identityStatus.equals("-1")) {
                        // 身份认证页面
                        startActivity(new Intent(getActivity(), MineMember.class));
                    } else if (identityStatus.equals("2")) {
                        Toast.makeText(getActivity(), "你的资料以在审核中,请耐心等待", Toast.LENGTH_SHORT).show();
                    } else {
                        // 一喂员认证页面
                        Intent ii = new Intent(getActivity(), MineAskNumberActivty.class);
                        ii.putExtra("gradeCode", gradeCode);
                        startActivity(ii);
                    }
                }

                break;
            case R.id.mine_rank:// 销售等级
                startActivity(new Intent(getActivity(), MineRank.class));
                break;
            case R.id.mine_recipients:// 收件人
                startActivity(new Intent(getActivity(), AddCloseActivity.class));
                break;
            case R.id.mine_money:// 保证金
                startActivity(new Intent(getActivity(), MineMoney.class));
                break;
            case R.id.mine_servicer://客服
                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.cancel_servicedialog, null);
                Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                TextView t1 = (TextView) view.findViewById(R.id.cancel_t1);
                TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                builder.setView(view);
                t1.setText("客服电话");
                t2.setText("4000051331");
                surebt.setText("呼叫");
                final AlertDialog alertDialog = builder.create();
                cancelbt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                surebt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:4000051331"));
                        startActivity(i);
                        alertDialog.dismiss();

                    }
                });
                alertDialog.show();
                break;
            case R.id.mine_view://建议
                startActivity(new Intent(getActivity(), MineView.class));
                break;
            case R.id.mine_help:// 帮助中心
                startActivity(new Intent(getActivity(), MineHelp.class));
                break;
            case R.id.mine_install:// 设置
                startActivity(new Intent(getActivity(), MineInstall.class));
                break;
        }
    }
}
